<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<!-- Casting SPELs in Lisp - Emacs Lisp Edition, a Comic Book
     Written by Conrad Barski, M.D., http://lisperati.com
     Edited by James A. Webb, http://uberkode.com -->

<head>

  <title>Casting SPELs in Lisp (24)</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="author" content="James A. Webb" />
  <meta name="date" content="2007-09-15T00:00:00+00:00" />
  <meta name="keywords" content="Emacs, Lisp" />

</head>

<body>

<center>

<table cellspacing="10" width="500">
  <tr>
    <td>
      Now we'll create a command to pickup objects in our world:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun pickup-object (object)
    (cond ((is-at object location object-locations)
           (push (list object 'body) object-locations)
           `(you are now carrying the ,object))
	   (t '(you cannot get that.))))
</pre>
    </td>
  </tr>
  <tr>
    <td>
      This function checks to see if the object is indeed on the
      floor of the current location- If it is, it pushes the new
      location (the player's body) onto the list (pushing means to
      add a new item to the list, in a way that the <tt>assoc</tt>
      command sees and therefore hides the previous location) and
      returns a sentence letting us know whether it succeeded.
    </td>
  </tr>
  <tr>
    <td>
      Now let's cast another SPEL to make the command easier to use:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defspel pickup (object)
    `(pickup-object ',object))
</pre>
    </td>
  </tr>
  <tr>
    <td>
      Now let's try our newest SPEL:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (pickup whiskey-bottle)
</pre>
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkgreen">
  ==> (you are now carrying the whiskey-bottle)
</pre>
    </td>
  </tr>
  <tr>
    <td>
      Now let's add a couple more useful commands. First, a command
      that lets us see our current inventory of items we're carrying:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun inventory ()
    (remove-if-not (lambda (x)
		     (is-at x 'body object-locations))
		   objects))
</pre>
    </td>
  </tr>
  <tr>
    <td>
      Now a function that tells us if we have a certain object with
      us:
    </td>
  </tr>
  <tr>
    <td>
<pre style="font-weight: bolder; color: darkblue">
  (defun have (object)
    (member object (inventory)))
</pre>
    </td>
  </tr>
  <tr>
    <td align="center">
      &lt;&lt; <a href="casting-spels-emacs-1.html">begin</a>
      &lt; <a href="casting-spels-emacs-23.html">previous</a> - 
      <a href="casting-spels-emacs-25.html">next</a> &gt;
      <a href="casting-spels-emacs-35.html" >end</a> &gt;&gt;
    </td>
  </tr>
</table>

</center>

</body>

</html>